package com.usian.fastdfs.config;

import com.usian.fastdfs.utils.FastDFSClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("file")
public class FileController {


    @Value("${fileServerUrl}")
    String fileServerUrl;

    @Autowired
    FastDFSClientUtil fastDFSClientUtil;

    @PostMapping("upload")
    public String uploadFile(MultipartFile file){
        try {
            //判断文件是否为空

            if (file==null){
                return "文件为空，无法上传";
            }
            //获取文件名
            String filename = file.getOriginalFilename();
            if (StringUtils.isEmpty(filename)){
                return "上传文件有误，文件名为空，请重新上传";
            }

            //上传文件    返回文件path

            String path = fastDFSClientUtil.uploadFile(file);
            //输出上传文件路径

            System.out.println(path);
            System.out.println(fileServerUrl+path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  "文件上传成功";

    }



    //图片删除
    @GetMapping("del")
    public  String delFile(String fileId){
        try {
            fastDFSClientUtil.delFile(fileId);
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
        return "删除成功";
    }
//
 //   group1/M00/00/00/wKjIgmJFGgGAfclEAAALNBb_d0A854.jpg
  //  http://192.168.200.130/group1/M00/00/00/wKjIgmJFGgGAfclEAAALNBb_d0A854.jpg
}













